﻿using NUnit.Framework;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Remote;
using System;
using TechTalk.SpecFlow;

//note on inconclusive and pending
//get browser running w/ default login page
//get is running w/ register page


namespace CERT.AcceptanceTests
{
    [Binding]
    public class RegisterSteps: FeatureBase
    {
        [Given(@"I am at the NCERT login page")]
        public void GivenIAmAtTheNCERTLoginPage()
        {
            var info = ScenarioContext.Current.ScenarioInfo;
            Assert.AreEqual("Requesting to register for NCERT", info.Title);
            if (CurrentDriver == null)
                CurrentDriver = (RemoteWebDriver)ScenarioContext.Current["CurrentDriver"];
            CurrentDriver.Navigate().GoToUrl("http://localhost:6014/");

            Assert.AreEqual(2, CurrentDriver.FindElementsByName("UserName").Count);
        }

        [When(@"I press Signup button")]
        public void WhenIPressSignupButton()
        {
            //signup
            if (CurrentDriver == null)
                CurrentDriver = (RemoteWebDriver)ScenarioContext.Current["CurrentDriver"];
            var SignButton = CurrentDriver.FindElementById("showRegister");

            if (SignButton == null)
                Assert.Fail("Expected to find a button with the value of 'showRegister'.");

            SignButton.Click();
        }


        [Then(@"I am at the registration page")]
        public void GivenIAmAtTheRegistrationPage()
        {
           // ScenarioContext.Current.Pending();
            var confirmInput = CurrentDriver.FindElementByName("ConfirmPassword");

            if (confirmInput == null)
                Assert.Fail("Expected to find a button with the value of 'confirmInput'.");
        }




        [Given(@"I am at registration")]
        public void GivenIAmAtRegistration()
        {
            var info = ScenarioContext.Current.ScenarioInfo;
            Assert.AreEqual("Registering for NCERT", info.Title);
            if (CurrentDriver == null)
                CurrentDriver = (RemoteWebDriver)ScenarioContext.Current["CurrentDriver"];
            CurrentDriver.Navigate().GoToUrl("http://localhost:6014/");

            Assert.AreEqual(2, CurrentDriver.FindElementsByName("UserName").Count);

            var SignButton = CurrentDriver.FindElementById("showRegister");

            if (SignButton == null)
                Assert.Fail("Expected to find a button with the value of 'showRegister'.");

            SignButton.Click();
        
            // ScenarioContext.Current.Pending();
            var confirmInput = CurrentDriver.FindElementByName("ConfirmPassword");

            if (confirmInput == null)
                Assert.Fail("Expected to find a button with the value of 'confirmInput'.");
        }

        [When(@"I fill in the following registration form")]
        public void WhenIFillInTheFollowingRegistrationForm(Table table)
        {
            if (CurrentDriver == null)
                CurrentDriver = (RemoteWebDriver)ScenarioContext.Current["CurrentDriver"];
            var pwrdGuid = new Guid();
            var pwrd = pwrdGuid.ToString().Substring(0, 7);
            var usr = "testor" + new Random(1).Next(10000).ToString();

            foreach (var row in table.Rows)
            {
                var textField = CurrentDriver.FindElementById(row["field"]);

                if (textField == null) Assert.Fail("Expected to find a text field with the name of '{0}'.", row["field"]);

                if (row["value"] == "testor1") row["value"] = usr;
                if (row["value"] == "password") 
                { row["value"] = pwrd; }
                

                textField.SendKeys(row["value"]);
            }

            var loginButton = CurrentDriver.FindElementByName("signup");

            if (loginButton == null)
                Assert.Fail("Expected to find a button with the value of 'singup'.");

            loginButton.Click();

        }

        [Then(@"I am at the home page")]
        public void ThenIAmAtTheHomePage()
        {
            var navBar = CurrentDriver.FindElementById("main-content");
            if (navBar == null)
                Assert.Fail("Expected to find the navbar");

        }
    }
}
